using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text.RegularExpressions;

namespace nToolbox.IO
{
	/// <summary>
	/// Parses command arguments
	/// </summary>
	public class CommandArguments
	{
		private StringDictionary _Arguments;

		public CommandArguments(IEnumerable<string> args)
		{
			Regex splitArgument = new Regex(@"^/|^-|=|:");

			foreach (string item in args)
			{
				string[] argumentParts = splitArgument.Split(item, 3);
				string argumentKey = string.Empty;
				string argumentValue = string.Empty;

				switch (argumentParts.Length)
				{
				case 3:
					argumentKey = argumentParts[1];
					argumentValue = argumentParts[2];
					break;
				case 2:
					argumentKey = argumentParts[1];
					argumentValue = "";
					break;
				}


				if (Arguments.ContainsKey(argumentKey))
				{
					Arguments.Remove(argumentKey);
				}

				if (argumentKey != string.Empty)
				{
					Arguments.Add(argumentKey, argumentValue.Replace("'", "").Replace("\"", ""));
				}
			}
		}

		public StringDictionary Arguments
		{
			get
			{
				if (_Arguments == null)
					_Arguments = new StringDictionary();

				return _Arguments;
			}
		}

		public string this[string key]
		{
			get { return Arguments[key] ?? string.Empty; }
		}
	}
}